package 地区数据;

import Moudle.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.ss.formula.functions.T;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ParseData {
    static City[] parseJsonStrToCity(String jsonStr){
        //解析json 转换为 city
        JSONArray return_data = JSONObject.parseObject(jsonStr).getJSONArray("returndata");
        JSONArray cities_json = return_data.getJSONObject(0).getJSONArray("nodes");

        City[] cities = new City[cities_json.size()];
        for (int i = 0; i < cities.length; i++) {
            JSONObject city_json = cities_json.getJSONObject(i);

            cities[i] = new City(city_json.getString("name"), city_json.getString("code"));
        }

        return cities;
    }

    static Item[] parseJsonStrToItem(String jsonStr) {
        //把json数据转换为Item类型
        JSONArray json = (JSONArray)JSON.parseArray(jsonStr);
        int size = json.size();
        Item[] items = new Item[size];
        for (int i = 0; i < size; i++) {
            JSONObject jsonObject =(JSONObject) json.get(i);
            String dbcode = jsonObject.getString("dbcode");
            String id = jsonObject.getString("id");
            String isParent = jsonObject.getString("isParent");
            String name = jsonObject.getString("name");
            String pid = jsonObject.getString("pid");
            String wdcode = jsonObject.getString("wdcode");
            items[i] = new Item(dbcode, id, isParent, name, pid, wdcode);
        }
        return items;
    }

    static String parseData(String jsonStr, City city, List<Excel_地区数据> excel_list, List<String> zb_list){
        /*
        * 解析表格数据 json转换为Excel_class
        * 返回值为 注：  的信息*/
        try {

        }catch (Exception e){

        }
        JSONObject return_data = JSONObject.parseObject(jsonStr).getJSONObject("returndata");

        //时间
        JSONArray timeArray = return_data.getJSONArray("wdnodes").getJSONObject(2).getJSONArray("nodes");
        //指标
        JSONArray zbArray = return_data.getJSONArray("wdnodes").getJSONObject(0).getJSONArray("nodes");
        //数据
        JSONArray dataArray = return_data.getJSONArray("datanodes");

        //data 解析  组成map
        Map<String, DataNode> dataNodeMap = new HashMap<>();
        int datanodes_len = dataArray.size();
        for (int i = 0; i < datanodes_len; i++){
            JSONObject jsonObject = dataArray.getJSONObject(i);
            DataNode dataNode = new DataNode(jsonObject.getString("code"),
                    jsonObject.getJSONObject("data").getString("hasdata"),
                    jsonObject.getJSONObject("data").getString("data"));
            dataNodeMap.put(dataNode.code, dataNode);
        }

        //指标 解析
        int zbArray_len = zbArray.size();
        ZB_Class[] zb_classes = new ZB_Class[zbArray_len];
        for (int i = 0; i < zbArray_len; i++) {
            JSONObject jsonObject = zbArray.getJSONObject(i);
            ZB_Class zb = new ZB_Class(jsonObject.getString("cname"),
                    jsonObject.getString("code"),
                    jsonObject.getString("memo"),
                    jsonObject.getString("unit"));

            if (!zb.unit.equals(""))
                zb.cname = zb.cname + zb.unit;

            zb_classes[i] = zb;
        }

        //时间 解析
        int timeArray_len = timeArray.size();
        Time_Class[] time_classes = new Time_Class[timeArray_len];
        for (int i = 0; i < timeArray_len; i++) {
            JSONObject jsonObject = timeArray.getJSONObject(i);
            Time_Class time_class = new Time_Class(jsonObject.getString("cname"),
                    jsonObject.getString("code"));

            time_classes[i] = time_class;
        }

        //写入excel每行的数据
        String ZHU = "";
        for (Time_Class time_class : time_classes) {
            String City_name = city.name;
            String Time = time_class.cname;

            List<String> list = new ArrayList<>();
            for (ZB_Class zb_class : zb_classes){
                if (!zb_list.contains(zb_class.cname))
                    zb_list.add(zb_class.cname);

                if (!ZHU.contains(zb_class.memo))
                    ZHU = ZHU + zb_class.memo + ";";

                String code = "zb." + zb_class.code + "_reg." + city.code + "_sj." + time_class.code;
                DataNode dataNode = dataNodeMap.get(code);
                String data = dataNode.hasData.equals("true") ? dataNode.data : "";
                if (!data.equals("")){
                    double v = Double.parseDouble(data);
                    DecimalFormat df = new DecimalFormat("0.00");
                    data = String.valueOf(df.format(v));
                }

                list.add(data);
            }

            excel_list.add(new Excel_地区数据(City_name, Time, list));
        }

        return ZHU;
    }


}
